package com.drools;

import com.example.droolsDemo.bean.Sku;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.kie.api.KieBase;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.utils.KieHelper;

public class Dtest {
    private ObjectMapper objectMapper = new ObjectMapper();

    @Test
    public void testOneSku() throws JsonProcessingException {
        Resource resource = ResourceFactory.newClassPathResource("rules/skupoints.drl");
        KieHelper kieHelper = new KieHelper();
        kieHelper.addResource(resource);
        KieBase kieBase = kieHelper.build();
        KieSession kieSession = kieBase.newKieSession();
        Sku sku1 = new Sku();
        sku1.setPrice(10);
        kieSession.insert(sku1);

        int allRules = kieSession.fireAllRules();
        kieSession.dispose();
        System.out.println("sku1:" + objectMapper.writeValueAsString(sku1));
        System.out.println("allRules:" + allRules);
    }

    @Test
    public void testOneSku2() throws JsonProcessingException {
        org.kie.api.io.Resource resource = ResourceFactory.newClassPathResource("rules/skupoints.drl");
        KieHelper kieHelper = new KieHelper();
        kieHelper.addResource(resource);
        KieBase kieBase = kieHelper.build();
        StatelessKieSession statelessKieSession = kieBase.newStatelessKieSession();
        Sku sku1 = new Sku();
        sku1.setPrice(10);
        statelessKieSession.execute(sku1);
        System.out.println("sku1:" + objectMapper.writeValueAsString(sku1));
    }
}
